<?php
class label_specialModel extends baseModel{
    protected $table = 'special_category';

    public function class_list($data){
        //解析
        $where=array();
        if(!empty($data['cid'])){
            $where['cid'] = ' AND cid in(' . $data['cid'] . ')';
        }
        if(!empty($data['pid'])){
            $where['pid'] = ' AND pid in(' . $data['pid']. ')';
        }
        if(!empty($data['type'])&&!empty($data['pid'])){
            $where['pid'] = ' AND pid in (' . model('special_category')->getcat($data['pid']) . ')';
        }
        if(!empty($data['att'])){
            if($data['att']=='list'){
                $where['type'] = ' AND type=1 ';
            }else{
                 $where['type'] = ' AND type=0 ';
            }
        }
        if (!empty($data['where'])) {
            $where['where'] = ' AND '.$data['where'];
        }
        if (!empty($data['limit'])) {
            $where['limit'] = $data['limit'];
        }else{
            $where['limit'] = 10;
        }
        if (!empty($data['order'])) {
            $where['order'] = $data['order'];
        }else{
            $where['order'] = 'cid desc';
        }

        $condition=$where['cid'].$where['pid'].$where['type'].$where['where'];
        $condition=substr($condition,4);
        $data=$this->select($condition,'',$where['order'],$where['limit']);
        if(!empty($data)){
            $i=0;
            foreach ($data as $key=>$value) {
                $i++;
                $list[$key]=$value;
                $list[$key]['curl']=$this->get_curl($value);
                $list[$key]['i']=$i;
            }
        }
        return $list;
    }

    //栏目超链接
    public function get_curl($data,$page=false)
    {
        $config=config('APP');
        if($config['URL_REWRITE_ON']){
            $rewrite = config('REWRITE');
            $rewrite = array_flip($rewrite);
            $url=$rewrite['special/category/index'];
            $parameter=array('cid'=>$data['cid']);
            if(!empty($url)){
                $parameter=array();
                if(strpos($url,'<cid>')){
                    $parameter['cid']=$data['cid'];
                }
                if(strpos($url,'<urlname>')){
                    $parameter['urlname']=$data['urlname'];
                }
            }
        }else{
            $parameter=array('cid'=>$data['cid']);
        }
        if($page){
            $pagearray=array('page'=>'{page}');
            $parameter=array_merge($parameter,$pagearray);
        }
        return url('special/category/index',$parameter);
    }

    public function special_list($data){
        //解析
        $where=array();
        if(!empty($data['cid'])){
            $where['cid']=' AND B.cid in(' . $data['cid'] . ')';
        }
        if ($data['type']=='sub'&&!empty($data['cid'])) {
            $where['cid'] = " AND B.cid in (" . model('special_category')->getcat($data['cid']).")";
        }
        if (!empty($data['image'])) {
            if($data['image']=='true')
            {
                $where['image'] = ' AND A.image<>"" ';
            }else{
                 $where['image'] = ' AND A.image is Null ';
            }
        }
        if (!empty($data['recommend'])) {
            $where['recommend'] = " AND R.pid in(".in($data['recommend']).') ';
        }
        if (!empty($data['where'])) {
            $where['where'] = ' AND '.$data['where'];
        }
        if (!empty($data['limit'])) {
            $where['limit'] = $data['limit'];
        }else{
            $where['limit'] = 10;
        }
        if(!empty($data['order'])){
            $where['order']='A.'.$data['order'].',';
        }
        //组合
        $condition='A.status=1'.$where['cid'].$where['image'].$where['recommend'].$where['where'];
        if($where['recommend']){
            $recommend="inner join {$this->model->pre}special_recommend R ON R.id = C.id";
        }
        //生成
        $sql="
        SELECT A.*,C.*,A.title as atitle,B.name as cname
        FROM {$this->model->pre}content A 
        inner join {$this->model->pre}special_list C ON A.aid = C.aid
        inner join {$this->model->pre}special_category B ON B.cid = C.cid
        {$recommend}
        WHERE {$condition}
        ORDER BY C.sequence ASC,{$where['order']}A.aid DESC
        LIMIT {$where['limit']}
        ";
        $data=$this->query($sql);
        if(!empty($data)){
            $i=0;
            foreach ($data as $key=>$value) {
                $i++;
                $list[$key]=$value;
                $list[$key]['aurl']=api('content','content_aurl',$value['aid']);
                $list[$key]['i']=$i;
            }
        }
        return $list;
    }


}

?>